import os
import sys

import time

CELERY_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../ihome'))
from celery import Celery

app = Celery('mycelery',broker='redis://127.0.0.1:6379/14', backend='redis://127.0.0.1:6379/15')

@app.task(bind=True)
def send_sms_code(self,mobile, sms_code, time_expire):
    from utils.yuntongxun.sms import CCP
    ccp = CCP()
    try:
        # 用res接受发送结果,成功为0,失败为-1
        res = ccp.send_template_sms(mobile, [sms_code, time_expire], 1)
    except Exception as e:
        print(e)
        res = -1
    if res == -1:
        # res如果是-1,即发送失败,重试
        self.retry(contdown=5,max_retries=3,exc=Exception('短信发送失败'))

